<?php
    /**
    * Datatype của module BĐS
    * Các datatype:
    *   - Collection của bđs
    *   - Dự án
    * 
    * @author trungdt
    * @since June 22, 2011
    */

    class bds_bds {
        // generate form for real estate project
        function form($object, $config, $loc) {
            if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
            
            $lang=exponent_lang_loadFile("modules/bdsmodule/default.php");

            exponent_forms_initialize();
            global $db;

            $form = new form();
            if (!isset($object->id)) {
                $object->name="";
                $object->col_id="";
                $object->province_id=-1;
                $object->province_ward_id=-1;
                // default to Gia Lai, VN :)
                $object->vitri_lat="14.058324";
                $object->vitri_long="108.277199";
                $object->vitri_zoom=5;
                $object->vitri_chitiet="";
                $object->thumbnail=-1;
                $object->featured=0;
                $object->bds_state=0;
                $object->noidung="";
                $object->hethan=strtotime("+7 days");

                $object->dientich="";
                $object->chieudai="";
                $object->chieurong="";
                $object->dientich_sudung="";
                $object->phaply=0;
                $object->huong=0;
                $object->cautruc="";
                $object->gia=0;
                $object->giaodich=0;
                $object->bds_dambao=0;
                $object->vitri="";
                
                // chi tiết phòng ốc
                $object->solau=1;
                $object->phongkhach=1;
                $object->phongngu=1;
                $object->phongtam=1;
                $object->phongkhac=1;
                
                // liên hệ
                $object->lienhe_ten="";
                $object->lienhe_phone="";
                $object->lienhe_mobile="";
                $object->lienhe_diachi="";

            } else {
                $form->meta('id',$object->id);
            }

            // permission checking
            $have_permission=false;
            if (exponent_users_isAdmin() || exponent_permissions_check('administrate',$loc) || exponent_permissions_check('create',$loc) || exponent_permissions_check('edit',$loc))
            {
                $have_permission=true;
            }
            // end of permission checking

            $form->register("name","Tiêu đề",new textcontrol($object->name,50,false,250,"",true));

            // giao dịch
            $giaodich=array();
            for ($i=0;$i<$lang['giaodich'];$i++)
            {
                $giaodich[$i]=$lang["{$i}giaodich"];
            }

            $form->register('giaodich','Loại tin đăng',new dropdowncontrol($object->giaodich,$giaodich));

            //
            $temp=serialize($loc);
            $cols=array();
            if ($have_permission)
            {
                $temp=$db->selectObjects("bds_collections"," location_data = '{$temp}' ORDER BY col_type, rank");
                
                foreach ($temp as $k=>$v)
                {
                    if ($v->col_type == 0)
                        $v->name="Rao vặt - ". $v->name;
                    else
                        $v->name="Đảm bảo - ". $v->name;
                    
                    $cols[$v->id]=$v->name;
                }
                
            }
            else
            {
                $cols=$db->selectDropdown("bds_collections","name"," (col_type = 0) AND location_data = '{$temp}'");
            }
            
            $form->register('col_id',"Loại BĐS",new dropdowncontrol($object->col_id, $cols));
            // for province and ward select
            $form->register("duan","",new rs_provinces($object->province_id, $object->province_ward_id));

            // thông tin chi tiết bất động sản
            $form->register("dientich","Diện tích (m<sup>2</sup>)",new textcontrol($object->dientich));
            $form->register("chieudai","Chiều dài (m)",new textcontrol($object->chieudai));
            $form->register("chieurong","Chiều rộng (m)",new textcontrol($object->chieurong));
            $form->register("dientich_sudung","Diện tích sử dụng (m<sup>2</sup>)",new textcontrol($object->dientich_sudung));

            // pháp lý
            $phaply=array();
            for ($i=0;$i<$lang['phaply'];$i++)
            {
                $phaply[$i]=$lang["{$i}phaply"];
            }

            $form->register('phaply','Pháp lý',new dropdowncontrol($object->phaply,$phaply));

            // hướng
            $huong=array();
            for ($i=0;$i<$lang['huong'];$i++)
            {
                $huong[$i]=$lang["{$i}huong"];
            }

            $form->register('huong','Hướng',new dropdowncontrol($object->huong,$huong));

            
            $sophong=array();
            for ($i=0;$i<11;$i++)
                $sophong[$i]=$i;
            
            $form->register('solau','Số lầu',new dropdowncontrol($object->solau,$sophong));
            $form->register('phongkhach','Số phòng khách',new dropdowncontrol($object->phongkhach,$sophong));
            $form->register('phongngu','Số phòng ngủ',new dropdowncontrol($object->phongngu,$sophong));
            $form->register('phongtam','Số phòng tắm',new dropdowncontrol($object->phongtam,$sophong));
            $form->register('phongkhac','Số phòng khác',new dropdowncontrol($object->phongkhac,$sophong));


            $form->register("gia","Giá (triệu đồng)",new textcontrol($object->gia));
            
            // liên hệ
            $form->register("lienhe_ten","Tên người liên hệ",new textcontrol($object->lienhe_ten));
            $form->register("lienhe_mobile","Di động người liên hệ",new textcontrol($object->lienhe_mobile));
            $form->register("lienhe_phone","Điện thoại bàn người liên hệ",new textcontrol($object->lienhe_phone));
            $form->register("lienhe_diachi","Địa chỉ người liên hệ",new textcontrol($object->lienhe_diachi));
            
            // nội dung
            if ($have_permission)
            {
                $form->register("noidung","Nội dung",new htmleditorcontrol($object->noidung));
            }
            else{
                $form->register("noidung","Nội dung",new texteditorcontrol($object->noidung));
            }
            
            $form->register("vitri","Vị trí bất động sản",new textcontrol($object->vitri));
            $form->register("duan_","Bấm vào chọn tọa độ của bất động sản",new rs_mapselect($object->vitri_long, $object->vitri_lat, $object->vitri_zoom, $config->map_key));
            // map control to select lat & long


            if ($have_permission)
                $form->register('thumbnail','Thumbnail', new ajax_select_file("thumbnail",$config->image_col,$object->thumbnail,true));
            else
            {
                $form->register('thumbnail','Thumbnail', new bds_fileuploadcontrol(true,"thumbnail",$config->image_col));
            }


            if ($have_permission)
            {
                $form->register('featured','Dự án nổi bật',new checkboxcontrol($object->featured));
                
                $form->register('bds_dambao','Bất động sản đảm bảo',new checkboxcontrol($object->bds_dambao));

                $bds_state=array();
                $bds_state[0]="Chờ duyệt";
                $bds_state[1]="Đã duyệt - Đang giao dịch";
                $bds_state[2]="Kết thúc giao dịch";

                $form->register('bds_state','Cho phép public dự án',new dropdowncontrol($object->bds_state,$bds_state));
            }

            // ngày hết hạn
            if ($have_permission)
            {
                $form->register('hethan',"Ngày hết hạn",new yuidatetimecontrol($object->hethan,"Ngày hết hạn", true, true, false, true));
            }

            $form->register('submit','',new buttongroupcontrol('Hoàn tất','','Hủy bỏ'));
            return $form;
        }

        function update($values,$object, $loc) {
            global $user;
            
            // permission checking
            $have_permission=false;
            if (exponent_users_isAdmin() || exponent_permissions_check('administrate',$loc) || exponent_permissions_check('create',$loc) || exponent_permissions_check('edit',$loc))
            {
                $have_permission=true;
            }
            
            $provine=duanmodule::_GetProvince_byID(intval($values['province_id']));
            $ward=duanmodule::_GetWard_byID(intval($values['province_ward_id']),$provine->id);

            $object->name=_ab_safe_html_string($values['name']);
            $object->col_id=intval($values['col_id']);
            $object->giaodich=intval($values['giaodich']);
            $object->sonha=_ab_safe_html_string($values['sonha']);
            $object->matduong=intval($values['matduong']);
            $object->tenduong=_ab_safe_html_string($values['tenduong']);
            $object->province_id=$provine->id;
            $object->province_ward_id=$ward->id;
            
            // giá
            $object->gia=_ab_safe_html_string($values['gia']);
            // giá type
            $object->gia_type=_ab_safe_html_string(strtolower($values['gia_type']));
            if (($object->gia_type !="vnd") || ($object->gia_type !="usd") || ($object->gia_type !="sjc"))
                $object->gia_type = "vnd";
            // cách tính dt, m2, thang
            $object->gia_cachtinh=_ab_safe_html_string(strtolower($values['gia_cachtinh']));
            if (($object->gia_cachtinh !="dt") || ($object->gia_cachtinh !="m2") || ($object->gia_cachtinh !="thang"))
                $object->gia_cachtinh = "dt";
            
            // môi giới
            if (isset($values['moigioi']))
                $object->moigioi=1;
            else
                $object->moigioi=0;
            $object->moigioi_chiphi=intval($values['moigioi_chiphi']);
            
            // dien tich su dng
            $object->dtsd=_ab_safe_html_string($values['dtsd']);
            $object->dtkv_dai=_ab_safe_html_string($values['dtkv_dai']);
            $object->dtkv_rong=_ab_safe_html_string($values['dtkv_rong']);
            if (isset($values['dtkv_nohau']))
                $object->dtkv_nohau=1;
            else
                $object->dtkv_nohau=0;
            $object->dtkv_nohaum=_ab_safe_html_string($values['dtkv_nohaum']);
        
            $object->dtxd_dai=_ab_safe_html_string($values['dtxd_dai']);
            $object->dtxd_rong=_ab_safe_html_string($values['dtxd_rong']);
            if (isset($values['dtxd_nohau']))
                $object->dtxd_nohau=1;
            else
                $object->dtxd_nohau=0;
            $object->dtxd_nohaum=_ab_safe_html_string($values['dtxd_nohaum']);
            
            // đặc điểm
            $object->phaply=intval($values['phaply']);
            $object->huong=intval($values['huong']);
            $object->duongtruocnha=intval($values['duongtruocnha']);
            $object->solau=intval($values['solau']);
            $object->sophongkhach=intval($values['sophongkhach']);
            $object->sophongngu=intval($values['sophongngu']);
            $object->sowc=intval($values['sowc']);
            $object->sophongkhac=intval($values['sophongkhac']);
        
            // tiện ích khác
            $object->tienich="";
            $temp=intval($values['tienich_c']);
            for ($i=0;$i<$temp;$i++){
                if (isset($values["tienich{$i}"]))
                    $object->tienich.="1";
                else
                    $object->tienich.="0";
            }
        
            // mô tả chi tiết bất động sản
            if ($have_permission)
                $object->mota=$values['mota'];
            else
                $object->mota=_ab_safe_html_string($values['mota']);
                 
            // default to Gia Lai, VN :)
            // vị trí
            $object->vitri_lat=$values['duan_lat'];
            $object->vitri_long=$values['duan_long'];
            $object->vitri_zoom=$values['duan_zoom'];
            
            $object->lienhe_ten=_ab_safe_html_string($values['lienhe_ten']);
            $object->lienhe_didong=_ab_safe_html_string($values['lienhe_didong']);
            $object->lienhe_dienthoai=_ab_safe_html_string($values['lienhe_dienthoai']);
            $object->lienhe_diachi=_ab_safe_html_string($values['lienhe_diachi']);
            $object->lienhe_ghichu=_ab_safe_html_string($values['lienhe_ghichu']);
            
            $object->hinhanh=_ab_safe_html_string($values['hinhanh']);
            
            // nổi bật
            global $db;
            $temp=$db->selectObject("bds_collections","id = {$object->col_id}");
            if ($temp)
                $object->bds_dambao=$temp->col_type;
            else
                $object->bds_dambao=0;
            
            if (!isset($object->id))
            {
                $object->ngaydang=time();
                global $user;
                $object->owner=$user->id;
                $object->bds_noibat=0;
            }
            
            if ($have_permission){
                $object->bds_trangthai=intval($values['bds_trangthai']);
                $object->hethan=strtotime($values['hethan_hidden']);
                if (isset($values['bds_noibat']))
                    $object->bds_noibat=1;
                else
                    $object->bds_noibat=0;
            }
            else
            {
                $object->bds_trangthai=0; // in every case else, change to trangthai to 0 mean waiting to approve
                $object->hethan=strtotime("+7 days");;
            }
            return $object;
        }

        /**
        * Build up form object for real estate project collections
        * 
        * @return form
        */
        function collections_form($object, $config=null) {
            if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
            exponent_forms_initialize();

            global $db;

            $form = new form();


            if (!isset($object->id)) {
                $object->name = '';
                $object->shortname='';
                $object->rank=0;
                $object->col_type=0; // =0: rao vặt; =1: đảm bảo
            } else {
                $form->meta('id',$object->id);
            }

            $col_type=array();
            $col_type[0]="Rao vặt";
            $col_type[1]="BĐS đảm bảo";

            $form->validationScript="";
            $form->register('name','Name',new textcontrol($object->name,50,false,200));
            $form->register('shortname','Shortname',new textcontrol($object->shortname,50,false,200));
            $form->register('col_type',"Loại BĐS",new dropdowncontrol($object->col_type, $col_type));
            $form->register('submit','',new buttongroupcontrol('Save','','Cancel'));
            return $form;
        }

        /**
        * This function update all info in real estate collections
        * @author: trungdt
        */
        function collections_update($values,$object) {
            $object->name = _ab_safe_html_string($values['name']);
            $object->shortname = _to_vietnamese_with_nomark(_ab_safe_html_string($values['shortname']));
            $object->col_type = intval($values['col_type']);
            return $object;
        }

    }

?>
